package network.盗版qq.qqserver.service;

import lombok.AllArgsConstructor;
import network.盗版qq.comment.Message;
import network.盗版qq.comment.MessageType;
import network.盗版qq.qqclient.service.ManageClientConnectServer;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author mubei
 * @date 2021/12/6
 * @description 该类对应的对象和某个客户端保持通信
 */
@AllArgsConstructor
public class ServerConnectClientThread {

    /*
     * 连接到userId的socket的
     * */
    private Socket socket;
    private String userId;

    /*
     * 发送接收消息
     * */
    public void run() {
                    while (true) {
                        try {
                            System.out.println("服务器客户端" + userId + "保持通信");
                            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                            Message msg = (Message) ois.readObject();
                            Message returnMessage = new Message();
                            // 获得全部在线人
                            if (msg.getMesType().equals(MessageType.GET_ONLINE_FRIEND)) {
                    System.out.println(msg.getSender() + " 想要在线用户列表");
                    String onlineUser = ManageServerClientThreads.getOnlineUser();
                    returnMessage.setContent(onlineUser);
                    returnMessage.setMesType(MessageType.RETURN_ONLINE_FRIEND);
                    returnMessage.setGetter(msg.getSender());
                    oos.writeObject(returnMessage);
                } else if (msg.getMesType().equals(MessageType.CLIENT_EXIT)) {
                    // 下线操作
                    System.out.println(msg.getSender() + " 下线了");
                    ManageClientConnectServer.removeServerConnectServerThread(msg.getSender());
                    // 关闭连接socket
                    socket.close();
                    // 退出线程
                    break;

                } else if (msg.getMesType().equals(MessageType.COMM_MES)) { // 给指定在线的人发送消息
                    // 消息转发
                    String getter = msg.getGetter();
                    // 如果不在线  可以离线留言
                    Socket socket = ManageServerClientThreads.getServerConnectClientThread(getter).getSocket();
                    ObjectOutputStream oos2 = new ObjectOutputStream(socket.getOutputStream());
                    oos2.writeObject(msg);
                } else if (msg.getMesType().equals(MessageType.COMM_MES_TO_ALL)) {  // 给全部在线的人发送消息
                    // 获取全部在在线人
                    String[] onlineUsers = ManageServerClientThreads.getOnlineUser().split(" ");
                    for (String onlineUser : onlineUsers) {
                        // 排除自己
                        if (!onlineUser.equals(msg.getSender())) {
                            Socket socket = ManageServerClientThreads.getServerConnectClientThread(onlineUser).getSocket();
                            ObjectOutputStream oos2 = new ObjectOutputStream(socket.getOutputStream());
                            oos2.writeObject(msg);
                        }
                    }
                } else {
                    // 处理其他状态
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public Socket getSocket() {
        return this.socket;
    }
}
